//
//  GYMineProductInfoDetailVC.m
//  GYDemo
//
//  Created by mac on 2018/3/30.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineProductInfoDetailVC.h"
#import "Head.h"

@interface GYMineProductInfoDetailHead : UIView<GCAdaptLabelDelegate,SDCycleScrollViewDelegate>
@property (nonatomic,strong) GYMineProductInfoDetailHeadViewModel *viewModel;
@property (nonatomic,strong) UIImageView *imageIV;
@property (nonatomic,strong) SDCycleScrollView *banners;
@property (nonatomic,strong) GCAdaptLabel *nameLB;
@property (nonatomic,strong) GCAdaptLabel *typeLB;
@property (nonatomic,strong) UILabel *timeLB;
@property (nonatomic,strong) UILabel *priceLB;
@property (nonatomic,strong) UILabel *setNumLB;
@property (nonatomic,strong) UILabel *saleCountLB;
@end

@implementation GYMineProductInfoDetailHead
#pragma mark - getter and setter
-(UIImageView *)imageIV
{
    if (!_imageIV) {
        _imageIV = [UIImageView new];
        _imageIV.backgroundColor = Main_Color_BG;
        _imageIV.width = self.width;
        _imageIV.height = WFCGFloatY(250);
        _imageIV.clipsToBounds = YES;
        _imageIV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageIV;
}
-(SDCycleScrollView *)banners
{
    if (!_banners) {
        _banners = [SDCycleScrollView new];
        _banners.width = self.width;
        _banners.height = WFCGFloatY(250);
        _banners.delegate = self;
    }
    return _banners;
}
-(GCAdaptLabel *)nameLB
{
    if (!_nameLB) {
        _nameLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(145.5)];
        _nameLB.x = WFCGFloatX(13);
        _nameLB.y = WFCGFloatY(260);
        _nameLB.width = WFCGFloatX(145.5);
        _nameLB.height = WFCGFloatY(21);
        _nameLB.font = SYSTEMFONT(15);
        _nameLB.textColor = Main_Color_Black;
        _nameLB.delegate = self;
    }
    return _nameLB;
}

-(GCAdaptLabel *)typeLB
{
    if (!_typeLB) {
        _typeLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(60)];
        _typeLB.x = WFCGFloatX(155);
        _typeLB.y = WFCGFloatY(15);
        _typeLB.width = WFCGFloatX(145.5);
        _typeLB.height = WFCGFloatY(14);
        _typeLB.font = SYSTEMFONT(10);
        _typeLB.textColor = Main_Color_Red;
        [WFFunctions WFUIaddbordertoView:_typeLB radius:WFCGFloatY(3) width:0.7f color:Main_Color_Red];
        _typeLB.delegate = self;
    }
    return _typeLB;
}

-(UILabel *)timeLB
{
    if (!_timeLB) {
        _timeLB = [UILabel new];
        _timeLB.frame = WFCGRectMake(13, 292, 260, 17);
        _timeLB.font = SYSTEMFONT(12);
        _timeLB.textColor = Main_Color_Gray;
    }
    return _timeLB;
}

-(UILabel *)priceLB
{
    if (!_priceLB) {
        _priceLB = [[UILabel alloc]init];
        _priceLB.width = WFCGFloatX(160);
        _priceLB.height = WFCGFloatY(29.5);
        _priceLB.x = SCREEN_WIDTH - WFCGFloatX(13) - _priceLB.width;
        _priceLB.y = WFCGFloatY(276);
        _priceLB.textColor = Main_Color_Red;
        _priceLB.font = SYSTEMFONT(20);
        _priceLB.textAlignment = NSTextAlignmentRight;
    }
    return _priceLB;
}
-(UILabel *)setNumLB
{
    if (!_setNumLB) {
        _setNumLB = [[UILabel alloc]init];
        _setNumLB.x = WFCGFloatX(13);
        _setNumLB.y = WFCGFloatY(332.5);
        _setNumLB.width = WFCGFloatX(160);
        _setNumLB.height = WFCGFloatY(18);
        _setNumLB.textColor = Main_Color_Gray;
        _setNumLB.font = SYSTEMFONT(12);
    }
    return _setNumLB;
}
-(UILabel *)saleCountLB
{
    if (!_saleCountLB) {
        _saleCountLB = [[UILabel alloc]init];
        _saleCountLB.width = WFCGFloatX(160);
        _saleCountLB.height = WFCGFloatY(18);
        _saleCountLB.x = self.width - WFCGFloatX(13) - _saleCountLB.width;
        _saleCountLB.y = WFCGFloatY(332.5);
        _saleCountLB.textColor = Main_Color_Gray;
        _saleCountLB.font = SYSTEMFONT(12);
        _saleCountLB.textAlignment = NSTextAlignmentRight;
    }
    return _saleCountLB;
}
-(void)setViewModel:(GYMineProductInfoDetailHeadViewModel *)viewModel
{
    if (viewModel) {
        _viewModel = viewModel;
        [_imageIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_viewModel.imgUrl[0],kProductImgOriginSuffix]]];
        NSMutableArray *imageArr = [NSMutableArray new];
        for (NSDictionary *dic in _viewModel.imgUrl)
        {
            NSString *picurl = dic[@"picurl"] ?: @"";
            NSString *pics = [NSString stringWithFormat:@"%@%@",picurl,kProductImgOriginSuffix];
            [imageArr addObject:pics];
        }
        _banners.imageURLStringsGroup = imageArr;
        _nameLB.text = _viewModel.name;
        _typeLB.text = [NSString stringWithFormat:@" %@ ",_viewModel.type];
        NSString *createtime = _viewModel.createtime;
        _timeLB.text = createtime;
        NSString *units = [NSString stringWithFormat:@"元/%@",_viewModel.unit];
        NSString *price = [NSString stringWithFormat:@"%@%@",_viewModel.price,units];
        NSMutableAttributedString *mprice = [[NSMutableAttributedString alloc]initWithString:price];
        [mprice addAttribute:NSFontAttributeName value:SYSTEMFONT(12) range:[price rangeOfString:units]];
        [mprice addAttribute:NSFontAttributeName value:SYSTEMFONT(20) range:[price rangeOfString:_viewModel.price]];
        _priceLB.attributedText = mprice;
        
        NSString *setNums = [NSString stringWithFormat:@"起订数量：%ld%@",self.viewModel.setNum.integerValue,self.viewModel.unit];
        NSString *saleCounts = [NSString stringWithFormat:@"累计成交量：%ld%@",self.viewModel.saleCount.integerValue,self.viewModel.unit];
        _setNumLB.text = setNums;
        _saleCountLB.text = saleCounts;
        [self layoutSubviews];
    }
}
#pragma mark - event
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = Main_Color_White;
        self.width = WF_UI_VIEW_WIDTH;
        self.height = WFCGFloatY(359.5);
        [self addSubview:self.imageIV];
        [self addSubview:self.banners];
        [self addSubview:self.nameLB];
        [self addSubview:self.typeLB];
        [self addSubview:self.timeLB];
        [self addSubview:self.priceLB];
        [self addSubview:self.setNumLB];
        [self addSubview:self.saleCountLB];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _typeLB.x = _nameLB.right + WFCGFloatX(5);
    _typeLB.centerY = _nameLB.centerY;
}
#pragma mark - delegate
#pragma mark - sd scroll delegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSMutableArray *marr = [NSMutableArray new];
//    NSMutableArray *cmarr = [NSMutableArray new];
    for (NSDictionary *dic in _viewModel.imgUrl) {
        NSString *picurl = dic[@"picurl"] ?: @"";
        NSString *pics = [NSString stringWithFormat:@"%@%@",picurl,kProductImgOriginSuffix];
        [marr addObject:pics];
//        UIImageView *tmpIV = [UIImageView new];
//        tmpIV.frame = _imageIV.frame;
//        [tmpIV sd_setImageWithURL:[NSURL URLWithString:pics] placeholderImage:DefaultImage];
//        [cmarr addObject:tmpIV];
    }
//    [[GCPresenter shareInterface] showOriginalImages:marr andImageViews:cmarr withIndex:index];
    [[GCPresenter shareInterface] showOriginalImages:marr andImageViews:nil withIndex:index];
}

-(void)callback:(id)sender
{
    [self layoutSubviews];
}
@end


@interface GYMineProductInfoDetailVC ()
@property (nonatomic,strong) GYMineProductInfoDetailViewModel *productInfoDetailViewModel;
@property (nonatomic,strong) GYMineProductInfoDetailHead *detailHead;
@end

@implementation GYMineProductInfoDetailVC
#pragma mark - getter and setter
-(GYMineProductInfoDetailViewModel *)productInfoDetailViewModel
{
    if (!_productInfoDetailViewModel) {
        _productInfoDetailViewModel = [GYMineProductInfoDetailViewModel new];
    }
    _productInfoDetailViewModel.productInfoModel = _productInfoModel;
    return _productInfoDetailViewModel;
}
-(GYMineProductInfoDetailHead *)detailHead
{
    if (!_detailHead) {
        _detailHead = [GYMineProductInfoDetailHead new];
    }
    return _detailHead;
}
#pragma mark - event
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"我的产品";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = self.view.height - 64;
    self.baseTable.noDataBtn.hidden = YES;
    if (@available(iOS 11.0, *)) {
        //        self.baseTable.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    }
    self.baseTable.mj_header.backgroundColor = Main_Color_Red;
    self.baseTable.scrollIndicatorInsets = self.baseTable.contentInset;
    [self.baseTable setTableHeaderView:self.detailHead];
    [self viewModelBand];
    if (_productInfoModel.down != 1) {
        [self judgeValue];
    }
    
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfp reloadData];
    }];
    
    [self reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.productInfoDetailViewModel;
    observation.keyPath = @"productInfoDetailModel";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation];
}
-(void)reloadView
{
    self.detailHead.viewModel = _productInfoDetailViewModel.productInfoDetailHeadViewModel;
    [self.baseTable reloadData];
}
-(void)reloadData
{
    [self.productInfoDetailViewModel requestDetail];
}

- (void)judgeValue {
    
    @WeakSelf;
    [self setRightBarButtonWithTitle:@"修改" titleColor:Main_Color_Red withBlock:^() {
        GYMineProductInfoEditVC *edit = [GYMineProductInfoEditVC new];
        edit.detailVC = selfp;
        edit.listVC = selfp.listVC;
        [edit.productInfoEditViewModel setProductInfoDetailModel:selfp.productInfoDetailViewModel.productInfoDetailModel];
        [selfp.navigationController pushViewController:edit animated:YES];
    }];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
#pragma mark - delegate
- (void)callback:(id)sender {
    [self performSelectorOnMainThread:@selector(reloadView) withObject:nil waitUntilDone:YES];
}
#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.productInfoDetailViewModel.allItems.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.productInfoDetailViewModel.allItems[section];
    return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineProductInfoEditDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineProductInfoEditDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    NSArray *items = self.productInfoDetailViewModel.allItems[indexPath.section];
    GYMineProductInfoEditDetailCellModel *model = items[indexPath.row];
    cell.titleLB.text = model.title;
    cell.textView.text = [NSString stringWithFormat:@"%@",model.value];
    cell.textView.editable = model.isEditable;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *items = self.productInfoDetailViewModel.allItems[indexPath.section];
    GYMineProductInfoEditDetailCellModel *model = items[indexPath.row];
    CGFloat height = model.cellHeight;
    return height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSArray *items = self.productInfoDetailViewModel.allItems[section];
    if (items.count > 1 && 0 == section) {
        return WFCGFloatY(45);
    }else{
        return 0.001f;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = Main_Color_White;
    view.width = self.view.width;
    view.height = WFCGFloatY(45);
    [WFFunctions WFUIaddBorderToView:view top:YES left:NO bottom:NO right:NO borderColor:Main_Color_BG borderWidth:0.7f];
    NSArray *items = self.productInfoDetailViewModel.allItems[section];
    if (items.count > 1 && 0 == section) {
        UILabel *titlelb = [UILabel new];
        titlelb.frame = WFCGRectMake(13, 0, 200, 45);
        titlelb.font = SYSTEMBOLDFONT(15);
        titlelb.textColor = Main_Color_Black;
        titlelb.text = @"产品详细信息";
        [view addSubview:titlelb];
        
        UIView *sep = [UIView new];
        sep.y = titlelb.bottom;
        sep.width = WFCGFloatX(20);
        sep.height = WFCGFloatY(1);
        sep.centerX = titlelb.centerX;
        sep.backgroundColor = Main_Color_BG;
        [view addSubview:sep];
    }
    return view;
}
#pragma mark - ace delegate
-(void)tableView:(UITableView *)tableView updatedHeight:(CGFloat)height atIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.productInfoDetailViewModel.allItems[indexPath.section];
    GYMineProductInfoEditDetailCellModel *cellmodel = arr[indexPath.row];
    if (cellmodel.isExpandable) {
        NSLog(@"updatedHeight %.2f",height);
        cellmodel.cellHeight = height;
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
